//package com.game.springbootstart.config;
//
//import com.game.springbootstart.entity.Admin;
//import com.game.springbootstart.exception.AccountNotFoundException;
//import com.game.springbootstart.service.IAdminService;
//import com.game.springbootstart.utils.JwtUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AuthenticationProvider;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//
//public class CustomAuthenticationProvider implements AuthenticationProvider {
//
//    @Autowired
//    private JwtUtil jwtUtil;
//
//    @Autowired
//    private IAdminService adminService;
//
//    @Override
//    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//        String token = (String) authentication.getCredentials();
//        if (jwtUtil.isTokenValid(token)) {
//            String username = JwtUtil.getUsernameFromToken(token);
//            Admin admin = adminService.findByUsername(username); // 获取用户信息
//            if (admin == null) {
//                throw new AccountNotFoundException("用户不存在");
//            }
//            return new UsernamePasswordAuthenticationToken(username, token, admin.getAuthorities());
//        }
//        throw new BadCredentialsException("无效的令牌");
//    }
//
//    @Override
//    public boolean supports(Class<?> authentication) {
//        return authentication.equals(UsernamePasswordAuthenticationToken.class);
//    }
//}
